<?php

namespace Admin\Controller;

class DriverController extends AdminController {

    public function index() {

        $tab = I('tab', 1, 'intval');

        if ($tab == 1) {
            $where['d.status'] = 1;
        } elseif ($tab == 2) {
            $where['d.status'] = 2;
        } else {
            $where['d.status'] = 0;
        }

        if (IS_CHEDUI) {
            $where['f.uid'] = UID;
        }

        $keywords = I('keywords');
        if ($keywords) {
            $map['d.driver_name'] = array('like', "%{$keywords}%");
            $map['d.driver_phone'] = array('like', "%{$keywords}%");
            $map['_logic'] = 'or';
            $where['_complex'] = $map;
            $this->assign('keywords', $keywords);
        }

        $M = M()->table("ot_driver as d")
                ->join('ot_fleet AS f on d.fleet_id = f.fleet_id');
        $field = 'd.*,f.fleet_name';
        $order = "d.create_time desc";
        $list = $this->lists($M, $where, $order, $field);

        $this->assign('tab', $tab);
        $this->assign('_list', $list);
        $this->display();
    }

    public function info() {
        $id = I('id', 0, 'intval');

        if ($id == 0) {
            $this->error('请选择司机');
        }

        if (IS_POST) {
            $data['eval_num'] = I('eval_num', 0);
            $data['update_time'] = date('Y-m-d H:i:s', time());

            $res = M('Driver')->where(['driver_id' => $id])->save($data);

            if ($res) {
                $this->success('执行成功', U('Driver/index', array('tab' => 1)));
            } else {
                $this->error();
            }
        } else {
            $data = M()->table("ot_driver as d")
                    ->field('d.*,f.fleet_name')
                    ->join('ot_fleet AS f on d.fleet_id = f.fleet_id')
                    ->where(['driver_id' => $id])
                    ->find();

            $this->assign('data', $data);
            $this->display();
        }
    }

    public function editstatus() {
        $id = I('id', 0, 'intval');

        if ($id == 0) {
            $this->error('请选择司机');
        }

        $status = I('status', 0, 'intval');
        $user_status = I('user_status', 0, 'intval');

        $res = 0;
        if ($status != 0) {
            $data['status'] = $status;
            $data['update_time'] = date('Y-m-d H:i:s', time());
            if ($status == 2) {
                $refund_desc = I('refund_desc');
                if (empty($refund_desc)) {
                    $this->error('请填写拒绝原因!');
                }
                $data['reason_text'] = $refund_desc;
                $data['refund_time'] = date('Y-m-d H:i:s', time());
            }
        }


        if ($user_status != 0) {
            if ($user_status == 2) {
                $user_status = 0;
            }

            $data['user_status'] = $user_status;
            $data['update_time'] = date('Y-m-d H:i:s', time());
        }

        $res = M('Driver')->where(['driver_id' => $id])->save($data);

        if ($res) {
            $this->success('执行成功', U('Driver/index', array('tab' => 1)));
        } else {
            $this->error();
        }
    }

    public function tixian() {
        $id = I('id', 0, 'intval');

        if ($id == 0) {
            $this->error('请选择司机');
        }

        $driver = M('Driver')->where(['driver_id' => $id])->find();

        if ($driver < $total_amount) {
            $this->error('余额不足,提现失败');
        }

        $total_amount = I('total_amount', 0);

        $data['driver_id'] = $id;
        $data['tx_price'] = $total_amount;

        $res = 0;
        $M = M();
        $M->startTrans();
        try {
            $res = M('DriverTx')->add($data);
            $res = M('Driver')->where(['driver_id' => $id])->setDec('driver_total_amount', $total_amount);
            $M->commit();
            $this->success('操作成功');
        } catch (Exception $ex) {
            $M->rollback();
            $this->error('操作失败');
        }
    }

}
